package hhtc.jsjds.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hhtc.jsjds.entity.ReviewscorePojo;
import hhtc.jsjds.service.ReviewscoreService;
import hhtc.jsjds.vo.FirstReviewScoreVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.List;

/**
 * (Reviewscore)表控制层
 *
 */
@Slf4j
@RestController
@RequestMapping("reviewscore")
public class ReviewscoreController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private ReviewscoreService reviewscoreService;

    /**
     * 分页查询所有数据
     *
     * @param page            分页对象
     * @param reviewscorePojo 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<ReviewscorePojo> page, ReviewscorePojo reviewscorePojo) {
        return success(this.reviewscoreService.page(page, new QueryWrapper<>(reviewscorePojo)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.reviewscoreService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param reviewscorePojo 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody ReviewscorePojo reviewscorePojo) {
        return success(this.reviewscoreService.save(reviewscorePojo));
    }

    /**
     * 修改数据
     *
     * @param reviewscorePojo 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody ReviewscorePojo reviewscorePojo) {
        return success(this.reviewscoreService.updateById(reviewscorePojo));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.reviewscoreService.removeByIds(idList));
    }

    //导出成绩
    @GetMapping("/export")
    public void exportScore(HttpServletResponse response) {
        OutputStream os = null;// 取得输出流
        try {
            os = response.getOutputStream();
            response.reset();
            response.setCharacterEncoding("UTF-8");
            //响应内容格式
            response.setContentType("application/vnd.ms-excel");
            String fileName = "初评成绩" + System.currentTimeMillis() + ".xlsx";
            response.setHeader("content-Disposition", "attachment;fileName=" + fileName);// 设定输出文件头，这里fileName=后面跟的就是文件的名称，可以随意更改
            List<FirstReviewScoreVO> exportResult = reviewscoreService.exportFirstReviewScore();
            EasyExcel.write(os, FirstReviewScoreVO.class).sheet("初评成绩").doWrite(exportResult);//sheet()里面的内容是工作簿的名称
        } catch (Exception e) {
            log.info("/reviewscore/export导出异常:{}", e);
        }
    }
}
